package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.AddRainObserverContract;
import com.sunlg.smarthome.data.entity.RainObserver;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

/**
 * Created by sunlg on 2017/6/2.
 */

public class AddRainObserverPresenter implements AddRainObserverContract.Presenter {

    private AddRainObserverContract.View mView;
    private SHServiceWrapper mSHServiceWrapper;
    private PreferHelper mPreferHelper;

    public AddRainObserverPresenter() {
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
        mPreferHelper = new PreferHelper(App.getInstance(), "gateway");
    }

    @Override
    public void addRainObserver(RainObserver rainObserver) {
        rainObserver.setGatewayId(mPreferHelper.getInt("id", 0));
        mSHServiceWrapper.addRainObserver(rainObserver,
                new MySubscriber<Result>() {
                    @Override
                    public void onSuccess(Result data) {
                        if(data.getCode() == C.ActionStatus.SUCCESS){
                            mView.addRainObserverSuccess();
                        }else {
                            _onError(new Throwable(data.getMsg()));
                        }
                    }

                    @Override
                    public void _onError(Throwable e) {
                        mView.showError(e.getMessage());
                    }
                });
    }

    @Override
    public void attachView(AddRainObserverContract.View view) {
        mView = view;
    }

    @Override
    public void dettachView() {
        mView = null;
    }
}
